/**
 * 道具
 */
class Item {
    constructor() {
		this.amount = 1;
		this.maxAmount = 1;
		this.price = 100;
		this.effects = [];
    }

	clone() {
		const clone = new Item();
		for(var property in this)
			clone[property] = this[property];

		return clone;
	}

	/**
	 * 获取道具Icon
	 * @returns 返回的Icon
	 */
	getIcon() {
		return new PIXI.Sprite(new PIXI.Texture(PIXI.Texture.from(`res/${this.icon}`), {x:this.iconSrcX+1,y:this.iconSrcY+1,width:this.iconWidth-2,height:this.iconHeight-2 }));
	}

	/**
	 * 使用道具
	 * @param {Character} character 被应用的角色
	 */
	use(character) {
		// 检查是否消耗，如果消耗则从背包中移除
		if(this.consume)
			gloabl.bag.remove(this.url, 1);

		// 触发使用效果
		for(var i = 0; i < this.effects.length; i++)
			this.effects[i](character);
	}

	/**
	 * 应用装备给角色
	 * @param {Character} character 被应用的角色
	 */
	 apply(character, location) {
		// 获取已装备道具
		const equip = character.equipments[location];

		// 应用当前装备
		character.equipments[location] = this;
		
		// 如果存在已装备道具则脱下(重新计算属性)
		if(equip) {
			character.hp -= equip.hp;
			character.sp -= equip.sp;
			character.atk -= equip.atk;
			character.def -= equip.def;
			character.speed -= equip.speed;
			character.keep -= equip.keep;
			character.range -= equip.range;
			character.weight -= equip.weight;

			// 更新道具数量
			global.bag.apply(equip.url, -1);
		}
		
		// 重新计算角色属性
		character.hp += this.hp;
		character.sp += this.sp;
		character.atk += this.atk;
		character.def += this.def;
		character.speed += this.speed;
		character.keep += this.keep;
		character.range += this.range;
		character.weight += this.weight; 

		// 更新道具数量
		global.bag.apply(this.url, 1);

		// 触发装备效果
		for(var i = 0; i < this.effects.length; i++)
			this.effects[i](character);
	}

	/**
	 * 为道具创建使用或装备时效果
	 * @param {Action<character>} effectFunc 
	 */
	createEffect(effectFunc) {
		this.effects.push(effectFunc);
	}
}

function ItemLoader(loader) {
    this.loader = loader;
}
ItemLoader.prototype = {
    responseType : 'text',
    load : function(buffer, params, enity) {
        const jsonObj = JSON.parse(buffer);
		enity.name = jsonObj.name;
		enity.descrption = jsonObj.descrption;
		enity.price = jsonObj.price;
		enity.weight = jsonObj.weight;
		enity.maxAmount = jsonObj.maxAmount;

		const iconSplits = jsonObj.iconBounds.split(",");
		enity.iconSrcX = parseInt(iconSplits[0]);
		enity.iconSrcY = parseInt(iconSplits[1]);
		enity.iconWidth = parseInt(iconSplits[2]);
		enity.iconHeight = parseInt(iconSplits[3]);
		enity.icon = jsonObj.icon;

		// 读取自定义属性
		for(var i = 0; i < jsonObj.properties.length; i++) {
			const property = jsonObj.properties[i];
			enity[property.name] = property.value;
		}

		// 加载不同道具类型属性
		enity.itemType = jsonObj.itemType;
		switch(enity.itemType) {
			case 0:
				this.loadEqu(enity, jsonObj);
				break;
			case 1:
				this.loadUse(enity, jsonObj);
				break;
			default:
				this.loadOther(enity, jsonObj);
				break;
		}
		
		// 加载道具脚本
		currentItem = enity;
		window.require(enity.url + ".fs.js");

        return enity;
    },
	loadEqu(enity, json) {
		enity.location = json.equipment.location;
		enity.speed = json.equipment.speed;
		enity.keep = json.equipment.keep;
		enity.range = json.equipment.range;
		enity.hp = json.equipment.hp;
		enity.sp = json.equipment.sp;
		enity.atk = json.equipment.atk;
		enity.def = json.equipment.def;
	},
	loadUse(enity, json) {
		enity.consume = json.usable.consume;
		enity.cd = json.usable.cd;
		enity.limit = json.usable.limit;
		enity.target = json.usable.target;
	},
	loadOther(enity, json) {

	},
    create : function(url) {
        const item = new Item();
		item.url = url;
		return item;
    }
}

L2D.Loader.addMode("item", new ItemLoader(this));